import service from './service'
import { CONTENT_TYPE } from '@/constants'
import { useUserStoreWithOut } from '@/store/modules/user'

const request = (option: AxiosConfig) => {
  const { url, method, params, data, headers, responseType } = option
  const userStore = useUserStoreWithOut()
  const { VITE_APP_CLIENT_ID } = import.meta.env
  // console.log(VITE_APP_CLIENT_ID,'VITE_APP_CLIENT_ID')
  // console.log(userStore.getTokenKey,userStore.getToken)
  return service.request({
    url: url,
    method,
    params,
    data,
    responseType: responseType,
    // headers: {
    //   'Content-Type': CONTENT_TYPE,
    //   "Authorization":'Bearer eyJraWQiOiIzNDBlN2IxNC0yMGI4LTQyZDMtYWJiZi1jMzI5MmU1NTVlODEiLCJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJhZG1pbmlzdHJhdG9yIiwiaXNBYmxlIjoxLCJpc3MiOiJodHRwczpcL1wvbG9naW4tdGVzdC50LWFhcm9uLmNvbSIsImF1ZCI6InR1b2hlbmctYWlycG9ydC1hZG1pbiIsIm5iZiI6MTcyNjc5Mzc0OSwib1VzZXJJZCI6NzQzLCJzY29wZSI6WyJ0dW9oZW5nLWFpcnBvcnQtYWRtaW4iLCJ0dW9oZW5nLWFpcnBvcnQtbXAiXSwiY2xpZW50Um9sZUxpc3QiOiJbe1wiY2xpZW50SWRcIjpcInR1b2hlbmctYWlycG9ydC1hZG1pblwiLFwicm9sZUlkXCI6MX0se1wiY2xpZW50SWRcIjpcInR1b2hlbmctYWlycG9ydC1tcFwiLFwicm9sZUlkXCI6MX1dIiwiZXhwIjoxNzI2ODgwMTQ5LCJpc0V4cGlyZSI6MSwiaWF0IjoxNzI2NzkzNzQ5LCJ1c2VybmFtZSI6ImFkbWluaXN0cmF0b3IifQ.wHw0BbNaLSacrmgZx41LclrDh0EfqMlSyjQi8XHLma0Zuuv6d-UU94zYf2cDoMsGkRl_1_xQBCqLKZXUABdhADoSVRIUpUBl3YoR9wnT9F-R2ASNsdG3-jYRxABXGB-1lC_hAiLWvctNbuf8vEfsIuogB61zuUdaNf1fFcaEzItuMBLGw_p0yJwZ_kbCrlRQpBr54IDRpSbWPvMB8w_fO414iR0kqUE1om4NSY2w-Yn41AMRDVb3nflpA5WRJxtsgjyvxtaYrkfSzg8n_k89IwaCsfArhKpLbO1Vf6U_EQgUAoP8gF6sNe49zyAKxp9oxb3IbxfAtBb04BUH46hSdQ',
    //   ['Client-Id']: VITE_APP_CLIENT_ID,
    //   ...headers
    // }
    headers: {
      'Content-Type': CONTENT_TYPE,
      [userStore.getTokenKey ?? 'Authorization']: userStore.getToken ?? '',
      ['Client-Id']: VITE_APP_CLIENT_ID,
      ...headers
    }
  })
}

export default {
  get: <T = any>(option: AxiosConfig) => {
    return request({ method: 'get', ...option }) as Promise<IResponse<T>>
  },
  post: <T = any>(option: AxiosConfig) => {
    return request({ method: 'post', ...option }) as Promise<IResponse<T>>
  },
  delete: <T = any>(option: AxiosConfig) => {
    return request({ method: 'delete', ...option }) as Promise<IResponse<T>>
  },
  put: <T = any>(option: AxiosConfig) => {
    return request({ method: 'put', ...option }) as Promise<IResponse<T>>
  },
  cancelRequest: (url: string | string[]) => {
    return service.cancelRequest(url)
  },
  cancelAllRequest: () => {
    return service.cancelAllRequest()
  }
}
